﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Disposables;
using Wulong.Phone.Dianping.Constants;
using Wulong.Phone.Dianping.Models;
using Wulong.Phone.Dianping.Utils;

namespace Wulong.Phone.Dianping.Services
{
    public class CityService
    {
        private static readonly string BaseUrl = CommonConstants.MobileHomePage + "/default.aspx?c=";

        private static readonly string[] Cities = 
            new[]
                    {
                    "上海|shanghai|SH|1|" + BaseUrl + "1",
                    "北京|beijing|BJ|2|" + BaseUrl + "2",
                    "杭州|hangzhou|HZ|3|" + BaseUrl + "3",
                    "广州|guangzhou|GZ|4|" + BaseUrl + "4",
                    "南京|nanjing|NJ|5|" + BaseUrl + "5",
                    "苏州|suzhou|SUZ|6|" + BaseUrl + "6",
                    "深圳|shenzhen|SZ|7|" + BaseUrl + "7",
                    "成都|chengdu|CD|8|" + BaseUrl + "8",
                    "重庆|chongqing|CQ|9|" + BaseUrl + "9",
                    "天津|tianjin|TJ|10|" + BaseUrl + "10",
                    "宁波|ningbo|NB|11|" + BaseUrl + "11",
                    "无锡|wuxi|WX|13|" + BaseUrl + "13",
                    "福州|fuzhou|FZ|14|" + BaseUrl + "14",
                    "厦门|xiamen|XM|15|" + BaseUrl + "15",
                    "武汉|wuhan|WH|16|" + BaseUrl + "16",
                    "西安|xian|XA|17|" + BaseUrl + "17",
                    "沈阳|shenyang|SY|18|" + BaseUrl + "18",
                    "大连|dalian|DL|19|" + BaseUrl + "19",
                    "济南|jinan|JN|22|" + BaseUrl + "22",
                    "长春|changchun|CC|70|" + BaseUrl + "70",
                    "青岛|qingdao|QD|21|" + BaseUrl + "21",
                    "哈尔滨|haerbin|HB|79|" + BaseUrl + "79",
                    "合肥|hefei|HF|110|" + BaseUrl + "110",
                    "南昌|nanchang|NC|134|" + BaseUrl + "134",
                    "郑州|zhengzhou|ZZ|160|" + BaseUrl + "160",
                    "长沙|changsha|CS|344|" + BaseUrl + "344",
                    "石家庄|shijiazhuang|SJZ|24|" + BaseUrl + "24",
                    "太原|taiyuan|TY|35|" + BaseUrl + "35",
                    "东莞|dongguan|DG|219|" + BaseUrl + "219",
                    "常州|changzhou|CZ|93|" + BaseUrl + "93"
                    };

        public static IObservable<City> FindCity(string keyword)
        {
            return new AnonymousObservable<City>(
                observer =>
                {
                    if (string.IsNullOrWhiteSpace(keyword))
                        observer.OnError(new Exception("No empty string allowed"));
                    foreach (var arrayOfCity in Cities.Select(city => 
                        city.Split('|'))
                            .Where(arrayOfCity => 
                            arrayOfCity[0].ToLower().StartsWith(keyword) ||
                            arrayOfCity[1].ToLower().StartsWith(keyword) ||
                            arrayOfCity[2].ToLower().StartsWith(keyword)))
                    {
                        observer.OnNext(new City
                                 {
                                     Name = arrayOfCity[0],
                                     Pinyin = arrayOfCity[1],
                                     Shortcut = arrayOfCity[2],
                                     Code = arrayOfCity[3],
                                     Link = arrayOfCity[4]
                                 });
                    }
                    observer.OnCompleted();
                    return Disposable.Empty;
                });
        }

        public static City DefaultCity()
        {
            return new City
                        {
                            Name = "上海",
                            Pinyin = "shanghai",
                            Shortcut = "SH",
                            Code = "1",
                            Link = BaseUrl+"1"
                        };
        }

        public static City GetCity(string code)
        {
            return Cities.Select(
                city => 
                    city
                    .Split('|'))
                    .Where(x => x[3].Equals(code))
                    .Select(arrayOfCity => 
                        new City
                        {
                            Name = arrayOfCity[0], 
                            Pinyin = arrayOfCity[1], 
                            Shortcut = arrayOfCity[2], 
                            Code = arrayOfCity[3], 
                            Link = arrayOfCity[4]
                        }).FirstOrDefault();
        }

        public static City GetCity(
            string code, 
            ObservableCollection<City> cities)
        {
            return cities
                    .Where(x => x.Code.Equals(code))
                    .FirstOrDefault();
        }

        public static IObservable<City> ObservableCity()
        {
            return new AnonymousObservable<City>(
                observer=>
                {
                    foreach (var arrayOfCity in Cities.Select(city =>
                        city.Split('|')))
                    {
                        observer.OnNext(new City
                        {
                            Name = arrayOfCity[0],
                            Pinyin = arrayOfCity[1],
                            Shortcut = arrayOfCity[2],
                            Code = arrayOfCity[3],
                            Link = arrayOfCity[4]
                        });
                    }
                    observer.OnCompleted();
                    return Disposable.Empty;
                });
        }
    }
}
